home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 343 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  71 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: b91926@fsgm01.fnal.gov (David Sachs)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: casting and virtual inheritence
  5. Date: 08 Feb 1996 14:56:31 PST
  6. Organization: FERMILAB, Batavia, IL
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4fdulf$34p@fsgm01.fnal.gov>
  9. References: <4fbt35$9ej@fido.asd.sgi.com>
  10. Reply-To: sachs@fnal.fnal.gov
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 8 Feb 1996 16:49:19 -0600
  13. X-Newsreader: NN version 6.5.0 #9 (NOV)
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMRp/rEy4NqrwXLNJAQHgcgH/Ts9VVT+AvAx7uxXQ6prWolPBuUKJiDZw
  16.     7lls2BsorWSYqivQ1hqgVBpfZVpFw6NjOPU+vsPoMC5yrIHFRPIvKA==
  17.     =+bcJ
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. cardboard.mti.sgi.com!box.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!chatz!chatz (David Chatterton) writes:
  21.  
  22. >Hi,
  23.  
  24. >There is very little on virtual inheritence in the draft. I cannot find nay
  25. >statement saying that you cannot do this:
  26.  
  27. >class A {};
  28. >class B : public virtual A {};
  29.  
  30. >int main()
  31. >{
  32. >    A* a1 = new A;
  33. >    A* a2 = new B;
  34. >    B* b1 = (B *)A;    // Illegal due to virtual inheritence
  35. >    return 0;
  36. >}
  37.  
  38. >Yet g++ and cfront won't let you. However, g++ (2.7.0) will let you do this:
  39.  
  40. >    B* b1 = dynamic_cast<B*>(A);
  41.  
  42. >Why can't you force the cast to a B (I guess it has to do with the virtual
  43. >tables)? And is g++ correct in allowing dynamic_cast to do it?
  44.  
  45. First of all, I think (B*A) or dynamic_cast<B*>(A) is not a valid
  46. expression, because A is a type name rather than a variable name. I
  47. assume you meant dynamic_cast<B*>(a1)... In this case the semantics
  48. are:
  49.  
  50.  
  51. class A {};
  52. class B : public virtual A {};
  53.  
  54. int main()
  55. {
  56.     A* a1 = new A;
  57.     A* a2 = new B;
  58.     B* b1 = dynamic_cast<B*>(a1);  // b1 == NULL, because there is no B
  59.     B* b2 = dynamic_cast<B*>(a2);  // points to a2 as a B
  60.     return 0;
  61. }
  62. -- 
  63. * IRS, IRS, lord of the federal tax, checking all income and every deduction, *
  64. * mailing out form 10 40, penalizing noncompliers, regulating, and auditing.  *
  65. David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
  66. Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
  67. ---
  68. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  69.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  70.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  71.